export default {
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      count: 0,
      identity: 1,
      name: '',
      type: 1
    }
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    //在缓存中获取值
    that.user_name = sessionStorage.getItem("user_name");
    that.user_id = sessionStorage.getItem("user_id");
    that.token = sessionStorage.getItem("access-token");
    if ((!that.token) || (!that.user_id) || (!that.user_name)) {
      that.$router.push('/login');
    }
    if (that.$route.query.identity) {
      that.type = parseInt(that.$route.query.identity);
    }
    //请求员工列表
    that.getList();
  },

  methods: {

    /**
     * 选择类型
     * @param tab
     */
    handleClick(tab) {
      let that = this;
      if (tab === 1) {
        that.type = 1;
      }
      if (tab === 2) {
        that.type = 2;
      }
      if (tab === 3) {
        that.type = 3;
      }
      if (tab === 4) {
        that.type = 4;
      }
      that.getList();
    },


    /**
     * 获取列表
     */
    getList() {
      var that = this;
      that.tableData = [];
      that.count = 0;
      //请求的数据
      var formData = {};
      formData.token = that.token;
      formData.id = that.user_id;

      //请求邀请者列表
      that.$http.post(that.adminApi.api_url + "/manager/index", formData, {
        emulateJSON: true
      }).then(
        function (res) {
          // 处理成功的结果
          if (res.body.list) {
            for (var i in res.body.list.list) {
              that.tableData.push({
                i: i,
                id: res.body.list.list[i].id,
                truename: res.body.list.list[i].truename,
                mobile: res.body.list.list[i].mobile,
                username: res.body.list.list[i].username,
                role_name: res.body.list.list[i].role_name,
                register_time: res.body.list.list[i].register_time,
              });
            }
            that.count = res.body.list.count;
          }
        });
    },


    /**
     * 下一页
     * @param currentPage
     */
    handleCurrentChange: function (currentPage) {
      var that = this;
      that.page = currentPage;
    },


    /**
     * 添加
     */
    add: function () {
      var that = this;
      that.$router.push({
        path: '/employee/add_employee'
      });
    },

    /**
     * 修改
     * @param id
     */
    edit: function (id) {
      var that = this;
      that.$router.push({
        path: '/employee/add_employee',
        query: {
          id: id
        }
      });
    },

    /**
     * 删除
     */
    deleteUser: function (e) {
      var that = this;
      that.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.doDelete(e);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //执行删除
    doDelete: function (e) {
      var that = this;
      //请求登陆接口
      that.$http.post(that.adminApi.api_url + "/Manager/del", {
        token: that.token,
        id: e
      }, {
        emulateJSON: true
      }).then(
        function (res) {
          // 处理成功的结果
          that.$message({
            type: 'success',
            message: `操作提示: ${ '删除成功' }`
          });
          that.getList();
        },
        function () {
          // 处理失败的结果
          that.$message({
            type: 'error',
            message: `操作提示: ${ '处理异常' }`
          });
        });
    }
  }
}